package com.abc.scholarship.controller;


import cn.hutool.core.bean.BeanUtil;
import com.abc.scholarship.common.enums.StatusEnum;
import com.abc.scholarship.domain.dto.CollegeDTO;
import com.abc.scholarship.domain.dto.ScholarshipDTO;
import com.abc.scholarship.domain.entity.Clazz;
import com.abc.scholarship.domain.entity.College;
import com.abc.scholarship.domain.entity.Scholarship;
import com.abc.scholarship.domain.page.PageDTO;
import com.abc.scholarship.domain.page.PageQuery;
import com.abc.scholarship.domain.vo.resp.ApiResult;
import com.abc.scholarship.service.MajorService;
import com.abc.scholarship.service.ScholarshipService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 奖学金表 前端控制器
 * </p>
 *
 * @author jzw
 * @since 2024-07-02
 */
@RestController
@RequestMapping("/scholarship")
@RequiredArgsConstructor
@Api(tags = "奖学金相关接口")
public class ScholarshipController {

    private final ScholarshipService scholarshipService;
    /**
     * 分页查询所有奖学金
     * @param page
     * @return
     */
    @GetMapping("/selectAll")
    @ApiOperation("分页查询所有奖学金")
    public ApiResult<PageDTO<Scholarship>> selectAll(PageQuery page) {
        Page<Scholarship> scholarshipPage = scholarshipService.page(page.toMpPage());
        return ApiResult.success(PageDTO.of(scholarshipPage, Scholarship.class));
    }

    /**
     * 根据 id查询奖学金
     * @param id
     * @return
     */
    @GetMapping("/select/{id}")
    @ApiOperation("根据 id查询奖学金")
    public ApiResult<Scholarship> selectById(@PathVariable Integer id) {
        return ApiResult.success(scholarshipService.getById(id));
    }

    /**
     * 修改数据
     *
     * @param scholarshipDTO 实体对象
     * @return 修改结果
     */
    @ApiOperation("修改奖学金信息")
    @PutMapping("/update")
    public ApiResult update(@RequestBody ScholarshipDTO scholarshipDTO) {
        scholarshipService.MUpdateById(BeanUtil.toBean(scholarshipDTO, Scholarship.class));
        return ApiResult.success();
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @ApiOperation("删除奖学金")
    @PutMapping("/delete")
    public ApiResult delete(@RequestParam("idList") List<Long> idList) {
        this.scholarshipService.setStatusByIds(idList, StatusEnum.NORMAL.getValue());
        return ApiResult.success("删除成功");
    }

    /**
     * 新增数据
     * @param scholarshipDTO
     * @return
     */
    @ApiOperation("新增奖学金")
    @PostMapping("/insert")
    public ApiResult insert(@RequestBody ScholarshipDTO scholarshipDTO) {
        String message = scholarshipService.insert(scholarshipDTO);
        if(message == null){
            return ApiResult.success("新增成功");
        }return ApiResult.fail(message);
//        scholarshipService.save(BeanUtil.toBean(scholarshipDTO, Scholarship.class));

    }
}

